package com.bluestone.extension.paros;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.parosproxy.paros.Constant;
import org.parosproxy.paros.control.Control;
import org.parosproxy.paros.core.proxy.ProxyListener;
import org.parosproxy.paros.model.Model;
import org.parosproxy.paros.network.HttpMessage;
import org.parosproxy.paros.network.SSLConnector;

import com.bluestone.scripts.ActionScript;

public class ProxyServer extends Thread implements ProxyListener,Runnable{

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ProxyServer proxy = new ProxyServer();
		proxy.run();

	}
	
	private static boolean shutdown = false;
	private static ProxyServer proxyServer = null;
	private Vector<ProxyListener> listeners = new Vector<ProxyListener>();
	
	public static ProxyServer getProxyServer(){
		if(proxyServer == null){
			proxyServer = new ProxyServer();
		}
		return proxyServer;
	}
	
	public void addListener(ProxyListener listener){
		System.out.println("add proxylistener "+listener);
		listeners.add(listener);
	}
	public void removeListener(ProxyListener listener){
		listeners.remove(listener);
	}
	
	public static void shutdown(){
		shutdown = true;
	}
	public void onHttpRequestSend(HttpMessage msg) {
		for(ProxyListener listener:listeners){
			listener.onHttpRequestSend(msg);
		}
		
	}

	public void onHttpResponseReceive(HttpMessage msg) {
		for(ProxyListener listener:listeners){
			listener.onHttpResponseReceive(msg);
		}				
	}
	public void run(ActionScript action){
		//handle config
		String proxyserver = action.getPara("proxyserver");
		String proxyport = action.getPara("proxyport");
		if(proxyserver==null){
			proxyserver = "localhost";
		}
		if(proxyport==null){
			proxyport = "8080";
		}
		try{
			File config = new File("xml\\config.xml");
			FileInputStream in = new FileInputStream(config);
			byte [] bytes = new byte[1024];
			int count = 0;
			StringBuffer buffer = new StringBuffer();
			while((count=in.read(bytes))>0){
				buffer.append(new String(bytes,0,count));
			}
			in.close();
			
			String data = buffer.toString();
			data = data.replace("$host$", proxyserver);
			data = data.replace("$port$", proxyport);
			
			File output = new File(System.getProperty("user.home")+"\\paros\\config.xml");
			output.getParentFile().mkdirs();
			
			FileOutputStream out = new FileOutputStream(output);
			out.write(data.getBytes());
			out.flush();
			out.close();
			
		}catch(Exception me){
			
		}
		run();
	}
	public void run() {
	    ProtocolSocketFactory sslFactory = null;
	    try {
	        Protocol protocol = Protocol.getProtocol("https");
	        sslFactory = protocol.getSocketFactory();
	    } catch (Exception e) {}
	    if (sslFactory == null || !(sslFactory instanceof SSLConnector)) {
	        Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new SSLConnector(), 443));
	    }
	    try {
			Model.getSingleton().getOptionsParam().load(Constant.getInstance().FILE_CONFIG);
		} catch (Exception e) {
			e.printStackTrace();
		}	    
	    Control.initSingletonWithoutView();
	    Control.getSingleton().getProxy().addProxyListener(this);
	    Control.getSingleton().getProxy().startServer();
	    

		
	}

}
